package cn.bugstack.mybatis.mapping;

import cn.bugstack.mybatis.cache.Cache;
import cn.bugstack.mybatis.executor.keygen.KeyGenerator;
import cn.bugstack.mybatis.scripting.LanguageDriver;
import cn.bugstack.mybatis.session.Configuration;

import java.util.List;

/**
 *     <select id="queryUserInfo" parameterType="cn.bugstack.mybatis.test.po.User" resultType="cn.bugstack.mybatis.test.po.User">
 *         SELECT id, name
 *         FROM user
 *         where id = #{id}
 *     </select>
 *     映射sql
 */
public class MappedStatement {

    private Configuration configuration;
    private SqlCommandType sqlCommandType;

    private Cache cache;

    private String id;
    private SqlSource sqlSource;

    private Class<?> resultType;

    private List<ResultMap> resultMaps;

    //针对selectKey 指定实体中的主键属性
    private String keyProperty;

    //针对insert语句 指定对应的主键生成器
    private KeyGenerator keyGenerator;
    private String[] keyProperties;
    private String[] keyColumns;
    private boolean flushCacheRequired;
    private boolean useCache;

    public LanguageDriver getLang() {
        return lang;
    }

    private LanguageDriver lang;

    public String[] getKeyProperties() {
        return keyProperties;
    }

    public KeyGenerator getKeyGenerator() {
        return keyGenerator;
    }

    public String[] getKeyColumns() {
        return keyColumns;
    }

    public boolean isFlushCacheRequired() {
        return flushCacheRequired;
    }

    public Cache getCache() {
        return cache;
    }

    // 内部类 Builder 实现建造者模式来创建 MappedStatement
    public static class Builder {
        private MappedStatement mappedStatement = new MappedStatement();

        public Builder(Configuration configuration,String id,SqlCommandType sqlCommandType,SqlSource sqlSource,Class<?> resultType) {
            mappedStatement.configuration = configuration;
            mappedStatement.id = id;
            mappedStatement.sqlCommandType = sqlCommandType;
            mappedStatement.sqlSource = sqlSource;
            mappedStatement.resultType = resultType;
            mappedStatement.lang = configuration.getLanguageRegistry().getDefaultDriver();

        }
        public MappedStatement build() {
            assert mappedStatement.id != null;
            return mappedStatement;
        }

        public String id() {
            return mappedStatement.id;
        }

        public void cache(Cache cache) {
            mappedStatement.cache = cache;
        }

        public void resultMaps(List<ResultMap> resultMaps) {
            mappedStatement.resultMaps = resultMaps;
        }


        public void keyGenerator(KeyGenerator keyGenerator) {
            mappedStatement.keyGenerator = keyGenerator;
        }
        public void keyProperty(String keyProperty) {
            if (keyProperty == null || keyProperty.trim().length() == 0) {
                mappedStatement.keyProperties = null;
            } else {
                mappedStatement.keyProperties = keyProperty.split(",");
            }
        }

        public void flushCacheRequired(boolean flushCache) {
            mappedStatement.flushCacheRequired = flushCache;
        }

        public void useCache(boolean useCache) {
            mappedStatement.useCache = useCache ;
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public SqlCommandType getSqlCommandType() {
        return sqlCommandType;
    }

    public void setSqlCommandType(SqlCommandType sqlCommandType) {
        this.sqlCommandType = sqlCommandType;
    }


    public SqlSource getSqlSource() {
        return sqlSource;
    }

    public void setSqlSource(SqlSource sqlSource) {
        this.sqlSource = sqlSource;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public Class<?> getResultType() {
        return resultType;
    }

    public List<ResultMap> getResultMaps() {
        return resultMaps;
    }


    public boolean isUseCache() {
        return useCache;
    }




}
